﻿using System;
using System.Globalization;
using Windows.UI.Xaml.Data;

namespace MarginCalc.Converters
{
    public class PercentConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return string.Format("{0:p}", value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            var s    = value as string;
            if (s != null)
            {
                var valor=s.Replace(CultureInfo.CurrentCulture.NumberFormat.PercentSymbol, "");
                decimal result;
                decimal.TryParse(valor, out result);
                return result/100m;
            }
            throw new NumberParseException();
        }
    }

    public class NumberParseException : Exception
    {
    }
}